﻿using System;
using System.Collections.Generic;
using System.Text;
using BF.IBLL.BaseSystem.AreaManagement;
using BF.IBLL.BaseSystem;
using BF.IDAL.BaseSystem;
using BF.Infrastructure;
using BF.Model;
using System.Linq.Expressions;


namespace BF.BLL.BaseSystem.AreaManagement
{
    public class Sys_AreaBLL : Isys_AreaBLL
    {
        private readonly Isys_AreaDAL iDal;
        private readonly IUnitOfWork<WSJD_BaseFrameworkEntities> uwork;
        public Sys_AreaBLL(IUnitOfWork<WSJD_BaseFrameworkEntities> uwork, Isys_AreaDAL iDal)
        {
            this.iDal = iDal;
            this.uwork = uwork;
            //this.uwork.LogEvent = new LogEventHandler(Sys_LogBLL.WriteLog);
        }

        #region Isys_AreaBLL 成员
        public string GetSubAreaCode(string areaCode, out int areaLevel)
        {
            string subAreaCode = areaCode;
            areaLevel = 3;
            if (string.IsNullOrEmpty(areaCode))
            {
                areaCode = "000000";
            }
            if (areaCode.Length == 6)
            {
                if (areaCode.Equals("000000"))
                {
                    areaLevel = 0;
                    subAreaCode = areaCode;
                }
                else if (areaCode.Substring(2, 4).Equals("0000"))//省级用户
                {
                    areaLevel = 1;
                    subAreaCode = areaCode.Substring(0, 2);
                }
                else
                    if (areaCode.Substring(4, 2).Equals("00"))//市级用户
                    {
                        areaLevel = 2;
                        subAreaCode = areaCode.Substring(0, 4);
                    }
                    else //县级用户
                    {
                        areaLevel = 3;
                        subAreaCode = areaCode;
                    }
            }
            else if (GetLevelByAreaCode(areaCode) == 4)
            {
                areaLevel = 4;
                subAreaCode = areaCode.Substring(0, 9);
            }
            else if (GetLevelByAreaCode(areaCode) == 5)
            {
                areaLevel = 5;
            }
            return subAreaCode;
        }

        /// <summary>
        /// 根据地区编码获得实体
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public sys_Area GetModelByID(string areaCode)
        {
            return iDal.GetModelByID(areaCode);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(Model.sys_Area model)
        {
            iDal.Add(model);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update()
        {
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="where"></param>
        /// <param name="orderBy"></param>
        /// <param name="pageModel"></param>
        /// <param name="isAscending"></param>
        /// <returns></returns>
        public List<sys_Area> GetList(Expression<Func<sys_Area, bool>> where, Expression<Func<sys_Area, object>> orderBy, PageModel pageModel, bool isAscending)
        {
            return iDal.GetList(where, orderBy, pageModel, isAscending);
        }
        /// <summary>
        /// 获取数据集
        /// </summary>
        /// <param name="where">查询条件</param>
        /// <returns>查询结果</returns>
        public List<sys_Area> GetList(Expression<Func<sys_Area, bool>> where)
        {
            return iDal.GetList(where);
        }
        /// <summary>
        /// 获取总数量
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public int GetRowCount(Expression<Func<sys_Area, bool>> where)
        {
            return iDal.GetRowCount(where);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(Model.sys_Area entity)
        {
            iDal.Delete(entity);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public bool Delete(System.Linq.Expressions.Expression<Func<Model.sys_Area, bool>> predicate)
        {
            iDal.Delete(predicate);
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 根据地区编码获取地区表实体后，获得iLevel的值
        /// </summary>
        /// <param name="areaCode"></param>
        /// <returns></returns>
        public int GetLevelByAreaCode(string areaCode)
        {
            return iDal.GetModelByID(areaCode).iLevel;
        }
        #endregion
    }
}
